package com.ttpai.boot.lbs;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.internal.path.CompiledPath;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by Kail on 2017/3/15.
 * Copyright(c) ttpai All Rights Reserved.
 */
public class Main {

    public static void httpTest() throws IOException {
        long l = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            System.out.println(i);

            Connection.Response execute = Jsoup.connect("http://pai.ttpai.cn/auctionDetail.html?auctionId=23728575").execute();
            System.out.println(execute.statusCode());
            execute.body();
        }
        System.out.println(System.currentTimeMillis() - l);
    }

    public static void main(String[] args) throws IOException {
        String json = "{\"status\":0,\"message\":\"query ok\",\"result\":{\"routes\":[{\"distance\":20123,\"duration\":75,\"duration_1m\":75,\"bounds\":\"40.014309000000004,116.285706,40.15301,116.318099\",\"steps\":[{\"mode\":\"WALKING\",\"distance\":760,\"duration\":11,\"direction\":\"东\",\"polyline\":[40.014634,116.312869,-135,160,-90,100,-40,80],\"steps\":[{\"instruction\":\"步行454米,左转进入中关村北大街\",\"polyline_idx\":[0,31],\"road_name\":\"\",\"dir_desc\":\"东南\",\"distance\":454,\"act_desc\":\"左转\"}]},{\"mode\":\"TRANSIT\",\"lines\":[{\"vehicle\":\"BUS\",\"id\":\"7225403257131337003\",\"title\":\"717\",\"station_count\":7,\"price\":2,\"destination\":{\"id\":\"5047847604295657152\",\"title\":\"地铁西二旗站\"},\"start_time\":\"05:30\",\"end_time\":\"22:00\",\"distance\":4560,\"duration\":26,\"polyline\":[40.016124,116.317759,423,-241,321,-214,410,-323,378],\"geton\":{\"id\":\"10640224892745223512\",\"title\":\"圆明园东门\",\"location\":{\"lat\":40.016124,\"lng\":116.317759}},\"getoff\":{\"id\":\"5047847604295657152\",\"title\":\"地铁西二旗站\",\"location\":{\"lat\":40.053226,\"lng\":116.304718}},\"stations\":[{\"id\":\"17005795237013591570\",\"title\":\"北京体育大学\",\"location\":{\"lat\":40.021902,\"lng\":116.314189}}]}]},{\"mode\":\"WALKING\",\"distance\":170,\"duration\":3,\"direction\":\"东\",\"polyline\":[40.053263,116.304691,66,158,10,50,0,50],\"steps\":[{\"instruction\":\"步行28米,左后转\",\"polyline_idx\":[0,9],\"road_name\":\"\",\"dir_desc\":\"东北\",\"distance\":28,\"act_desc\":\"左后转\"}]},{\"mode\":\"TRANSIT\",\"lines\":[{\"vehicle\":\"SUBWAY\",\"id\":\"6754388447601197938\",\"title\":\"地铁昌平线\",\"station_count\":4,\"price\":5,\"destination\":{\"id\":\"5906130352814166149\",\"title\":\"昌平西山口\"},\"start_time\":\"05:50\",\"end_time\":\"23:05\",\"distance\":13569,\"duration\":27,\"polyline\":[40.052861,116.306361,1204,-672,4714,-2712,2866,-1811],\"geton\":{\"id\":\"7710841192752225287\",\"title\":\"西二旗\",\"location\":{\"lat\":40.052861,\"lng\":116.306361},\"exit\":{\"id\":5331673106458131000,\"title\":\"A1口(北口)\"}},\"getoff\":{\"id\":\"5906130352814166149\",\"title\":\"沙河\",\"location\":{\"lat\":40.148216,\"lng\":116.288868},\"exit\":{\"id\":1125299843369869000,\"title\":\"B1口(东口)\"}},\"stations\":[{\"id\":\"16352832121503765012\",\"title\":\"生命科学园\",\"location\":{\"lat\":40.094756,\"lng\":116.294197}}]}]}]},{\"distance\":20009,\"duration\":85,\"bounds\":\"40.014309000000004,116.285706,40.152254,116.318099\"},{\"distance\":22707,\"duration\":90,\"bounds\":\"40.014309000000004,116.262268,40.15301,116.341639\"}]}}";
        JsonPath duration = JsonPath.compile("$.result.routes[0].duration");
        JsonPath distance = JsonPath.compile("$.result.routes[0].distance");

        Logger logger = LoggerFactory.getLogger(CompiledPath.class);

        long l = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            DocumentContext context = JsonPath.parse(json);
//            System.out.println(context.read(duration));
//            System.out.println(context.read(distance));
            context.read(duration);
            context.read(distance);
        }
        System.out.println("耗时");
        System.out.println(System.currentTimeMillis() - l);


    }

}
